package com.hdy.orderforthree.mvp;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.hdy.net.order.bean.BasicDataResponse;
import com.hdy.net.order.bean.CategoryBean;
import com.hdy.net.order.bean.DishBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;
import com.hdy.orderforthree.adapter.BaseAdapter;
import com.hdy.orderforthree.adapter.FoodAdapter;
import com.hdy.orderforthree.adapter.FoodCategoryAdapter;
import com.hdy.orderforthree.pos.ConcretePosFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class FoodView extends BaseView implements IFoodContract.View {

    @BindView(R2.id.food_category_recyclerView) RecyclerView mFoodCategoryRecyclerView;
    @BindView(R2.id.food_recyclerView) RecyclerView mFoodRecyclerView;

    private TitleBarPresenter.UpdateViewCallback mUpdateViewCallbackCallback;

    private FoodCategoryAdapter mFoodCategoryAdapter;
    private FoodAdapter mFoodAdapter;

    private BasicDataResponse mFood;

    private final int MaxCount =1;
    private int mNextCount = 0;
    private int mPreCount = 0;

    public FoodView(View view) {
        super(view);

        mFood = new BasicDataResponse();
        mFood.setCategory(new ArrayList<>());
        mFood.setDishes(new ArrayList<>());
        mFood.setCooks(new ArrayList<>());
        mFood.setReasons(new ArrayList<>());
        mFood.setMaps(new HashMap());

        initView();
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mView.getContext());
        mFoodCategoryRecyclerView.setLayoutManager(layoutManager);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(mView.getContext(), 3);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        mFoodRecyclerView.setLayoutManager(gridLayoutManager);
        mFoodRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //自动翻页功能注销
                /*if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!recyclerView.canScrollHorizontally(1)) {//下一个分类
                        int position = mFoodCategoryAdapter.getSelectedPosition() + 1;
                        if (position < mFood.getCategory().size()) {
                            if (mNextCount >= MaxCount) {
                                changeCategory(position);
                                clearCount(false);
                            } else {
                                nextCategoryCount();
                            }
                        } else {
                            Toast.makeText(mView.getContext(), mView.getContext().getString(R.string.bottom_toast), Toast.LENGTH_SHORT).show();//滑动到底部
                        }
                    }
                    if (!recyclerView.canScrollHorizontally(-1)) {//上一个分类
                        int position = mFoodCategoryAdapter.getSelectedPosition() - 1;
                        if (position >= 0) {
                            if (mPreCount >= MaxCount) {
                                changeCategory(position);
                                clearCount(true);
                            } else {
                                preCategoryCount();
                            }
                        } else {
                            Toast.makeText(mView.getContext(), mView.getContext().getString(R.string.top_toast), Toast.LENGTH_SHORT).show();//滑动到顶部
                        }
                    }
                }*/
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }

            private void changeCategory(int position) {
                CategoryBean bean = (CategoryBean) mFood.getCategory().get(position);
                String next = mView.getContext().getString(R.string.change_category);
                Toast.makeText(mView.getContext(), String.format(next, bean.getName()), Toast.LENGTH_SHORT).show();//切换分类
                loadFoodData(position);
            }

        });
    }

    private void clearCount(boolean isUp) {
        mNextCount = 0;
        if (isUp) {
            mPreCount++;
        } else {
            mPreCount = 0;
        }
    }

    private void nextCategoryCount() {
        mPreCount = 0;
        mNextCount++;
    }

    private void preCategoryCount() {
        mPreCount++;
        mNextCount = 0;
    }

    public void setUpdateViewCallback(TitleBarPresenter.UpdateViewCallback cb) {
        mUpdateViewCallbackCallback = cb;
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showData(Object obj) {
        setFood((BasicDataResponse) obj);
        loadCategoryData();
        loadFoodData(mFoodCategoryAdapter.getSelectedPosition());
    }

    private void setFood(BasicDataResponse obj) {
        BasicDataResponse food = obj;
        food.analyse();
        mFood.getCategory().clear();
        mFood.getCategory().addAll(food.getCategory());
        mFood.getMaps().clear();
        mFood.getMaps().putAll(food.getMaps());
    }

    private void loadCategoryData() {
        if (mFoodCategoryAdapter == null) {
            mFoodCategoryAdapter = new FoodCategoryAdapter(mFood.getCategory());
            mFoodCategoryAdapter.setSelectedPosition(0);
            mFoodCategoryRecyclerView.setAdapter(mFoodCategoryAdapter);
            mFoodCategoryAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<CategoryBean>() {
                @Override
                public void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position) {
                    loadFoodData(position);
                }

                @Override
                public void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, CategoryBean bean) {

                }

                @Override
                public void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, CategoryBean bean) {

                }

                @Override
                public void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, CategoryBean bean) {

                }
            });
        }
        mFoodCategoryAdapter.notifyDataSetChanged();
    }

    private void loadFoodData(int position) {
        mFoodCategoryAdapter.notifyItemChanged(mFoodCategoryAdapter.getSelectedPosition());
        mFoodCategoryAdapter.notifyItemChanged(position);
        mFoodCategoryRecyclerView.smoothScrollToPosition(position);

        mFoodCategoryAdapter.setSelectedPosition(position);

        if (mFoodAdapter == null) {
            if (mFood.getCategory().size() > 0) {
                String key = ((CategoryBean)mFood.getCategory().get(position)).getCode();
                mFoodAdapter = new FoodAdapter(mFood.getMaps().get(key));
                mFoodRecyclerView.setAdapter(mFoodAdapter);
                mFoodAdapter.setSelectedPosition(0);
                mFoodAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<DishBean>() {
                    @Override
                    public void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position) {

                    }

                    @Override
                    public void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dish) {
                        ConcretePosFactory.getInstance().getPos().addCart(dish);
                        updateFoodView(position);
                    }

                    @Override
                    public void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dish) {
                        ConcretePosFactory.getInstance().getPos().removeCart(dish);
                        updateFoodView(position);
                    }

                    @Override
                    public void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, DishBean dishBean) {

                    }

                    private void updateFoodView(int position) {
                        showCategoryCorner(position);
                        if (mUpdateViewCallbackCallback != null) {
                            mUpdateViewCallbackCallback.showTotalOrderNumberCorner();
                        }
                        mFoodAdapter.notifyItemChanged(position);
                    }
                });
                mFoodAdapter.notifyDataSetChanged();
                mFoodRecyclerView.smoothScrollToPosition(0);
            }
        } else {
            String key = ((CategoryBean)mFood.getCategory().get(position)).getCode();
            mFoodAdapter.SetList(mFood.getMaps().get(key));
            mFoodAdapter.setSelectedPosition(0);
            mFoodAdapter.notifyDataSetChanged();
            mFoodRecyclerView.smoothScrollToPosition(0);
        }
    }

    private void showCategoryCorner(int position) {
        if (mFood.getCategory().size() > 0 && mFood.getMaps().size() > 0) {
            CategoryBean categoryBean = (CategoryBean) mFood.getCategory().get(mFoodCategoryAdapter.getSelectedPosition());
            computeOrderQty(categoryBean);

            if (mFoodCategoryAdapter != null) {
                mFoodCategoryAdapter.notifyItemChanged(mFoodCategoryAdapter.getSelectedPosition());
            }

            if (mFoodAdapter != null) {
                mFoodAdapter.notifyItemChanged(position);
            }
        }
    }

    private void computeOrderQty(CategoryBean categoryBean) {
        List<DishBean> list = mFood.getMaps().get(categoryBean.getCode());
        float number = 0;
        for (DishBean dishBean : list) {
            number = number + dishBean.getQty();
        }
        categoryBean.setOrderNumber(number);
    }

    /**
     * 熟悉点餐数量
     */
    private void refreshQty() {
        if (mFood.getCategory().size() > 0 && mFood.getMaps().size() > 0) {
            for (Object obj : mFood.getCategory()) {
                CategoryBean categoryBean = (CategoryBean) obj;
                computeOrderQty(categoryBean);

            }
        }
    }

    @Override
    public void showFailureMessage(String msg) {

    }

    @Override
    public void showErrorMessage() {
    }

    public void notifyDataSetChanged() {
        refreshQty();
        if (mFoodCategoryAdapter != null) {
            mFoodCategoryAdapter.notifyDataSetChanged();
        }
        if (mFoodAdapter != null) {
            mFoodAdapter.notifyDataSetChanged();
        }
    }
}
